﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Fireball.OpenXML.Word
{
    #region ParagraphIndentation
    /// <summary>
    /// Store the information about the indentation for a paragraph
    /// </summary>
    public sealed class ParagraphIndentation : IOpenXMLEntity
    {
        /// <summary>
        /// FirstLine additional indentation in Twentieths of a Point
        /// </summary>
        public decimal? FirstLine
        {
            get;
            set;
        }

        /// <summary>
        /// FirstLine additional indentation in character decimal units
        /// </summary>
        public decimal? FirstLineChars
        {
            get;
            set;
        }

        /// <summary>
        /// Indentation removed from FirstLine, Measurement in Twentieths of a Point
        /// </summary>
        public decimal? Hanging
        {
            get;
            set;
        }


        /// <summary>
        /// Indentation removed from FirstLine, in character decimal units
        /// </summary>
        public decimal? HangingChars
        {
            get;
            set;
        }

        /// <summary>
        /// Left indentation signed in Twentieths of a Point
        /// </summary>
        public decimal? Left
        {
            get;
            set;
        }

        /// <summary>
        /// Left indentation signed in character decimal units
        /// </summary>
        public decimal? LeftChars
        {
            get;
            set;
        }


        /// <summary>
        /// Right indentation signed in Twentieths of a Point
        /// </summary>
        public decimal? Right
        {
            get;
            set;
        }

        /// <summary>
        /// Right indentation signed in character decimal units
        /// </summary>
        public decimal? RightChars
        {
            get;
            set;
        }

        private void ParseInternal(XElement xInd)
        {
            if (xInd.Name.LocalName != "ind")
                throw new Exception("Invalid Node for Indentation");

            var firstLine = xInd.Attribute(NS.WM + "firstLine");

            if (firstLine != null)
            {
                this.FirstLine = Convert.ToDecimal(firstLine.Value);
            }

            var firstLineChars = xInd.Attribute(NS.WM + "firstLineChars");

            if (firstLineChars != null)
            {
                this.FirstLineChars = Convert.ToDecimal(firstLineChars.Value);
            }

            var hanging = xInd.Attribute(NS.WM + "hanging");

            if (hanging != null)
            {
                this.Hanging = Convert.ToDecimal(hanging.Value);
            }

            var hangingChars = xInd.Attribute(NS.WM + "hangingChars");

            if (hangingChars != null)
            {
                this.HangingChars = Convert.ToDecimal(hangingChars.Value);
            }

            var left = xInd.Attribute(NS.WM + "left");

            if (left != null)
            {
                this.Left = Convert.ToDecimal(left.Value);
            }

            var leftChars = xInd.Attribute(NS.WM + "leftChars");

            if (leftChars != null)
            {
                this.LeftChars = Convert.ToDecimal(leftChars.Value);
            }

            var right = xInd.Attribute(NS.WM + "right");

            if (right != null)
            {
                this.Right = Convert.ToDecimal(right.Value);
            }

            var rightChars = xInd.Attribute(NS.WM + "rightChars");

            if (rightChars != null)
            {
                this.RightChars = Convert.ToDecimal(rightChars.Value);
            }
        }

        public static ParagraphIndentation Parse(XElement xInd)
        {
            var p = new ParagraphIndentation();

            p.ParseInternal(xInd);

            return p;
        }
             

        #region IOpenXMLEntity Members

        public string Name
        {
            get;
            set;
        }

        public System.Xml.Linq.XContainer ToXml()
        {
            XElement xInd = new XElement(NS.WM + "ind");

            if (this.FirstLine != null)
                xInd.Add(new XAttribute(NS.WM + "firstLine", this.FirstLine.Value));

            if (this.FirstLineChars != null)
                xInd.Add(new XAttribute(NS.WM + "firstLineChars", this.FirstLineChars.Value));

            if (this.Hanging != null)
                xInd.Add(new XAttribute(NS.WM + "hanging", this.Hanging.Value));

            if (this.HangingChars != null)
                xInd.Add(new XAttribute(NS.WM + "hangingChars", this.HangingChars.Value));

            if (this.Left != null)
                xInd.Add(new XAttribute(NS.WM + "left", this.Left.Value));

            if (this.LeftChars != null)
                xInd.Add(new XAttribute(NS.WM + "leftChars", this.HangingChars.Value));

            if (this.Right != null)
                xInd.Add(new XAttribute(NS.WM + "right", this.Right.Value));

            if (this.RightChars != null)
                xInd.Add(new XAttribute(NS.WM + "rightChars", this.RightChars.Value));

            return xInd;
        }

        #endregion
    }
    #endregion
}
